/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.service.impl;

import com.alicp.jetcache.anno.CachePenetrationProtect;
import com.alicp.jetcache.anno.CacheRefresh;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.bifrost.entity.CountryArea;
import com.bifrost.repository.CountryAreaRepository;
import com.bifrost.service.CountryAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author bifrost
 * @version 1.0.0
 * @Date 2017/10/17 15:30
 */
@Service
public class CountryAreaServiceImpl implements CountryAreaService {
    @Autowired
    CountryAreaRepository countryAreaRepository;

    /**
     * 查询所有省包含直辖市
     *
     * @return
     */
    @Override
    public List<CountryArea> getAllStates() {
        return countryAreaRepository.findByDepth(1);
    }

    /**
     * 查询所有2级市
     *
     * @return
     */
    @Override
    public List<CountryArea> getAllCities() {
        return countryAreaRepository.findByDepth(2);
    }

    /**
     * 查询指定省的二级市
     *
     * @param state
     * @return
     */
    @Override
    public List<CountryArea> getCitiesWith(CountryArea state) {
        return countryAreaRepository.findAllByParent(state);
    }

    /**
     * 获取地区树
     *
     * @return
     */
    @Override
    @Cached(cacheType = CacheType.REMOTE,name ="countryAreas",expire =30 ,timeUnit = TimeUnit.DAYS)
    public List<Map> getAreasTree() {
        List<CountryArea> states = getAllStates();
        List<Map> list = new ArrayList<>(states.size());
        states.forEach(state -> {
            Map stateMap = new HashMap(3);
            list.add(stateMap);
            stateMap.put("n", state.getName());
            stateMap.put("v", state.getId());
            //获取市
            List<CountryArea> cities = state.getChildren();
            List<Map> citieList = new ArrayList<>(cities.size());
            stateMap.put("c", citieList);
            cities.forEach(city -> {
                Map cityMap = new HashMap(3);
                citieList.add(cityMap);
                cityMap.put("n", city.getName());
                cityMap.put("v", city.getId());
                //区
                List<CountryArea> districts = city.getChildren();
                List<Map> districtList = new ArrayList<>(districts.size());
                cityMap.put("c", districtList);
                districts.forEach(district -> {
                    Map districtMap = new HashMap(3);
                    districtList.add(districtMap);
                    districtMap.put("n", district.getName());
                    districtMap.put("v", district.getId());
                });
            });
        });
        return list;
    }

    /**
     * 获取指定id 的区域
     *
     * @param id
     * @return
     */
    @Override
    public CountryArea getArea(Long id) {
        return countryAreaRepository.findOne(id);
    }

    /**
     * 返回详细地址
     *
     * @param state
     * @param city
     * @param district
     * @param street
     * @return
     */
    @Override
    public String getAddrDetail(CountryArea state, CountryArea city, CountryArea district, String street) {
        StringBuilder builder = new StringBuilder();
        if(state != null){
            CountryArea stateCA = countryAreaRepository.findOne(state.getId());
            if(stateCA != null){
                builder.append(stateCA.getName());
            }
        }
        if(city != null){
            CountryArea cityCA = countryAreaRepository.findOne(city.getId());
            if(cityCA != null){
                builder.append(cityCA.getName());
            }
        }
        if(district != null){
            CountryArea districtCA = countryAreaRepository.findOne(district.getId());
            if(districtCA != null){
                builder.append(districtCA.getName());
            }
        }
        return builder.append(street).toString();
    }
}
